package com.dst.smart.handler.disposer;

import android.content.Context;
import android.util.Log;
import com.dst.smart.app.MainActivity;
import com.dst.smart.handler.entity.SemanticResult;
import com.dst.smart.handler.entity.ServiceType;
import com.dst.smart.music.MusicPlayMedia;
import com.dst.smart.player.entity.SongPlayInfo;
import org.json.JSONArray;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;

/**
 * 每天一句英语
 * 我要学英语
 来个英语句子
 学英语
 * author <a href="http://www.xfyun.cn">讯飞开放平台</a>
 * date 2016年9月5日 下午4:58:10
 *
 */
public class EnglishEveryDayDisposer extends Disposer {
	//private static final String KEY_URL = "url";

	public EnglishEveryDayDisposer(Context context) {
		super(context);
	}
	
	@Override
	public void disposeResult(final SemanticResult semanticResult) {
		try {
			if (semanticResult != null) {
				JSONArray result = semanticResult.getResult();
				if (result != null && result.length() != 0) {
					List<SongPlayInfo> playList = new ArrayList<SongPlayInfo>();
					for (int index = 0; index < result.length(); index++) {
						SongPlayInfo songPlayInfo = new SongPlayInfo();
						JSONObject newsJson = result.optJSONObject(index);
						String url = newsJson.getString("url");
						String translation = newsJson.getString("translation");
						songPlayInfo.setAnswerText(translation);
						songPlayInfo.setPlayUrl(url);
						playList.add(songPlayInfo);
					}
					try {
						if (MusicPlayMedia.getInstansMedia() == null) {
							new MusicPlayMedia(mContext, "");
						}
						MusicPlayMedia.getInstansMedia().playURLList(playList);
						MusicPlayMedia.getInstansMedia().setFileName(1,
								playList.get(0).getPlayUrl(), 1);
						//MainActivity.playSound(5, semanticResult.getAnswerText());
						MusicPlayMedia.getInstansMedia().play();
						if (semanticResult.getAnswerText().length() > 0) {
							//先播放標題
							getPlayController().playURLList(semanticResult.getUUID(), null);
							getPlayController().playText(semanticResult.getUUID(), semanticResult.getAnswerText(), true, "", null);
						}
					} catch (Exception e) {
						// TODO: handle exception
					}
					sendAiuiData("每日一句英语", "englishEveryday", result.toString());
				} else {
					getPlayController().playURLList(semanticResult.getUUID(), null);
					getPlayController().playText(semanticResult.getUUID(), semanticResult.getAnswerText(), true, "", null);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
	
	public boolean canHandle(ServiceType type){
		return (type == ServiceType.ENGLISHEVERYDAY);
	}
}
